package com.xsh.opensltest.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import androidx.core.content.ContextCompat;

import com.chengtao.pianoview.entity.Piano;
import com.chengtao.pianoview.listener.OnPianoListener;
import com.chengtao.pianoview.view.PianoView;
import com.xsh.opensltest.R;
import com.xsh.opensltest.SlEngine;
import com.xsh.opensltest.utils.DisplayUtils;
import com.xsh.opensltest.utils.FullException;
import com.xsh.opensltest.utils.MidiUtils;

public class PluginPiano extends LinearLayout
{
    private String tag = PluginPiano.class.getSimpleName();
    private int scrollProgress = 0;
    private SeekBar sb;
    // 此变量需要重构
    private int progress;

    public PluginPiano(Context context)
    {
        super(context);
        setOrientation(LinearLayout.VERTICAL);
        LinearLayout toolBar = new LinearLayout(context);
        toolBar.setOrientation(LinearLayout.HORIZONTAL);
        Button leftArrowButton = new Button(context);
        sb = new SeekBar(context);
        Button rightArrowButton = new Button(context);
        Drawable leftArrow = ContextCompat.getDrawable(context, R.drawable.left_arrow);
        Drawable rightArrow = ContextCompat.getDrawable(context,R.drawable.right_arrow);
        leftArrowButton.setLayoutParams(new ViewGroup.LayoutParams(DisplayUtils.dpToPx(40, context), DisplayUtils.dpToPx(40,context)));
        rightArrowButton.setLayoutParams(new ViewGroup.LayoutParams(DisplayUtils.dpToPx(40,context),DisplayUtils.dpToPx(40,context)));
        leftArrowButton.setBackground(leftArrow);
        rightArrowButton.setBackground(rightArrow);
        sb.setBackground(ContextCompat.getDrawable(context,R.drawable.piano_bar));
        LinearLayout.LayoutParams sb_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        sb_params.weight = 1;
        sb.setThumb(ContextCompat.getDrawable(context,R.drawable.seekbar_thumb));
        sb.setProgressDrawable(null);
        sb.setLayoutParams(sb_params);
        PianoView piano = new PianoView(context);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                piano.scroll(i);
            }@Override public void onStartTrackingTouch(SeekBar seekBar) { }@Override public void onStopTrackingTouch(SeekBar seekBar) { }});

        ViewGroup.LayoutParams piano_params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, // 宽度设为匹配父容器
                DisplayUtils.dpToPx(100,context) // 高度设为 200 像素
        );

        piano.setLayoutParams(piano_params);
        toolBar.addView(leftArrowButton);
        toolBar.addView(sb);
        toolBar.addView(rightArrowButton);
        addView(toolBar);
        addView(piano);
        bindPianoEvent(piano);
    }

    private void bindPianoEvent(PianoView pianoView)
    {
        pianoView.setPianoListener(pianoListener);
    }
    OnPianoListener pianoListener = new OnPianoListener()
    {
        @Override
        public void onPianoInitFinish()
        {
            Log.d(tag,"钢琴初始化完成");
        }

        @Override
        public void onPianoClick(Piano.PianoKeyType pianoKeyType, Piano.PianoVoice pianoVoice, int group, int positionOfGroup)
        {
            Log.d(tag,"called from: " + this);
            Log.d(tag,"钢琴按下: " + pianoKeyType + "钢琴音" + pianoVoice);
            int midiNote = MidiUtils.toMidiNoteNumber(pianoKeyType,pianoVoice,group,positionOfGroup);
            Log.d(tag,"midi键盘:" + midiNote);
            // 这行代码报错
            try {
                SlEngine.activeNoteOn(midiNote);
            }catch (Exception e)
            {
                FullException.print(e);
            }
        }
    };
}
